﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.ClientDW.DataAccessLayer.ESGRatings;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer;
using Sustainalytics.DataLayer.EsgRatings;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities.ESGRatings;
using Sustainalytics.ESGRatings.BusinessLogic;

namespace Sustainalytics.ESGRatings.Service.Tests
{
    [TestClass]
    public class ReferenceUniversesManagementTests
    {
        [TestMethod]
        public void test()
        {
            var clientPlatformConnectionString = "mongodb://54.72.160.155:27017/ClientPlatform";
            var clientDWConnectionString = "mongodb://54.72.160.155:27017/ClientDW";
            var redisConnectionString = "54.72.160.155";

            var waitObj = new ManualResetEvent(false);

            var esgInputFileRepository =
                EsgInputFileRepository.GetInstance(new FileRepository(clientPlatformConnectionString),
                    new RepositoryCollection<EsgInputFile>(clientPlatformConnectionString));

            var dwRepository = ClientDwRepository.GetInstance(clientDWConnectionString);
            var referenceUniversesRepository = ReferenceUniversesRepository.GetInstance(clientDWConnectionString);

            var referenceUniverseManagement = ReferenceUniversesManagement.GetInstance(esgInputFileRepository,
                dwRepository, referenceUniversesRepository);
            IEnumerable<string> resultMessage = referenceUniverseManagement.ProcessLoadedReferenceUniverse();
        }

        [TestMethod]
        public void ProcessLoadedReferenceUniverse()
        {
            var fileContent = File.ReadAllBytes("/Reference universes.xlsx");
            var unprocessFiles = new List<EsgInputFileComplete>
            {
                new EsgInputFileComplete
                {
                    Content = fileContent,
                    Id = Guid.NewGuid(),
                    Type = EsgInputFileType.ReferenceUniverse,
                    FileName = "Reference universes.xlsx",
                    Processed = null,
                    Name = "ref universe 1",
                    CreationTimestamp = DateTime.UtcNow
                }
            };
            var companyId1 = Guid.NewGuid();
            var companyId2 = Guid.NewGuid();
            var refUniverse = new ReferenceUniverse
            {
                Id = Guid.NewGuid(),
                Name = "ref universe 1",
                Companies = new List<ReferenceUniverseCompany>
                {
                    new ReferenceUniverseCompany(companyId1, 123, "Company1"),
                    new ReferenceUniverseCompany(companyId2, 456, "Company2")
                }
            };
            var companies = new List<CorporateData>
            {
                new CorporateData {CapitalIqId = 123, CompanyName = "Company1", Id = companyId1},
                new CorporateData {CapitalIqId = 456, CompanyName = "Company2", Id = companyId2}
            };

            var clientDw = new Mock<IClientDwRepository>();
            clientDw.Setup(m => m.GetCorporateDataByCapitalIQID(It.IsAny<List<string>>())).Returns(
                (List<string> listOfGuids) => companies);

            var esgInputFileRepository = new Mock<IEsgInputFileRepository>();
            esgInputFileRepository.Setup(m => m.
                ListLatestUnprocessedFiles(It.IsAny<EsgInputFileType>()))
                .Returns((EsgInputFileType type) => unprocessFiles);

            var referenceUniversesRepository = new Mock<IReferenceUniversesRepository>();
            referenceUniversesRepository.Setup(m => m.GetReferenceUniverseByName(It.IsAny<string>())).Returns(
                (string name) => refUniverse)
                ;

            var refUniverseManagement = ReferenceUniversesManagement.GetInstance(esgInputFileRepository.Object,
                clientDw.Object, referenceUniversesRepository.Object);

            var result = refUniverseManagement.ProcessLoadedReferenceUniverse();

            Assert.IsTrue(result.Any());
        }
    }
}